﻿using System;
using System.Data;
using DevMaker.SIC.Model.BaseObject;
using DevMaker.SIC.Util;
using log4net;


namespace DevMaker.SIC.Model {
    [Serializable]
    public class Usuario : NamedObject {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Apellido del usuario
        /// </summary>
        public String Apellido { set; get; }

        public String NombreCompleto {
            get { return Nombre + " " + Apellido; }
        }

        /// <summary>
        /// Identifica si un usuario esta activo
        /// </summary>
        public Boolean Activo { set; get; }

        /// <summary>
        /// Codigo del usuario en Payroll
        /// </summary>
        public String Rut { set; get; }

        /// <summary>
        /// Idenfitifacor en Vista del usuario
        /// </summary>
        public String UserNo { set; get; }

        /// <summary>
        /// Password del usuario, Equivale al campo     
        /// </summary>
        public String Password { set; get; }

        public String CodigoTarjeta { get; set; }

        private String UserGStrCode { set; get; }

        /// <summary>
        /// Tipo del usuarios
        /// </summary>
        public TipoUsuario TipoUsuario { set; get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="suffix"></param>
        /// <param name="reader"></param>
        public void ReadFromDataReader(String suffix, IDataReader reader) {
            try {
                Id = Convert.ToInt32(reader[suffix + "User_intUserNo"]);
                Nombre = StringUtil.UppercaseFirst(reader[suffix + "User_strFirstName"].ToString().ToLower().Trim());
                Apellido = StringUtil.UppercaseFirst(reader[suffix + "User_strLastName"].ToString().ToLower().Trim());
                if (reader[suffix + "User_strEnabled"].ToString() == "Y")
                    Activo = true;

                Rut = reader[suffix + "User_strHOPayrollCode"].ToString();
                UserNo = reader[suffix + "User_intUserNo"].ToString();
                Password = reader[suffix + "User_intPIN"].ToString();
                if (reader.HasColumn(suffix + "User_strSecurityId"))
                    CodigoTarjeta = reader[suffix + "User_strSecurityId"].ToString();

                UserGStrCode = reader[suffix + "UserG_strCode"].ToString();
                TipoUsuario = TipoUsuario.NoDefinido;

                if (UserGStrCode.ToUpper() == "EMP")
                    TipoUsuario = TipoUsuario.Cajero;

                if (UserGStrCode.ToUpper() == "SUP")
                    TipoUsuario = TipoUsuario.Supervisor;

                if (UserGStrCode.ToUpper() == "MGR" || UserGStrCode.ToUpper() == "MAN")
                    TipoUsuario = TipoUsuario.Admnistrador;
            } catch (Exception ex) {
                Log.Error(ex);
            }
        }

        public void ReaderSupervisorFromDataReader(IDataReader reader) {
            ReadFromDataReader("supervisor_", reader);
        }
        public void ReaderCajeroFromDataReader(IDataReader reader) {
            ReadFromDataReader("cajero_", reader);
        }

        public void ReadFromDataReader(IDataReader reader) {
            ReadFromDataReader(String.Empty, reader);
        }
    }
}
